package com.aishua.aishuawallet.activity;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.aishua.aishuawallet.Constant;
import com.aishua.aishuawallet.R;
import com.aishua.aishuawallet.business.TaskBusiness;
import com.aishua.aishuawallet.engin.IResponseListener;
import com.aishua.aishuawallet.longconnection.ClientSocket;
import com.aishua.aishuawallet.longconnection.SocketFactory;
import com.aishua.aishuawallet.mina.MianManager;
import com.aishua.aishuawallet.mina.MinaResultLIstener;
import com.aishua.aishuawallet.utils.BitmapUtils;
import com.aishua.aishuawallet.utils.CacheUtils;
import com.aishua.aishuawallet.utils.LogUtils;

import org.apache.mina.core.session.IoSession;

/**
 * Created by aishua on 2018/8/2.
 */

public class RegisthreeActivity extends BaseActivity {

    private static final String TAG = "RegisthreeActivity-";
    private Context mContext;
    private Toolbar tl_regist;
    private ImageView img_registone;
    private ImageView img_registwo;
    private ImageView img_registhree;
    private TextView tv_registone;
    private TextView tv_registwo;
    private TextView tv_registhree;
    private EditText et_pw;
    private EditText et_pwagain;
    private EditText et_email;
    private String phone;
    private String code;
    private Button btn_finish;
    private String pw;
    private String pwagain;
    private String email;
    private int REGIST_SUCCESS = 1;
    private int REGIST_FAIL = 2;
    private ClientSocket clientSocket;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    finishRegist();
                    break;
                case 2:
                    Toast.makeText(mContext, "注册失败", Toast.LENGTH_SHORT).show();
                    break;
            }


        }
    };

    @Override
    public  void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registhree);

        mContext = this;

        initToolbar();
        phone = getIntent().getStringExtra("phone");
        code = getIntent().getStringExtra("code");

        initView();
        initRegistNumview();
        business();
    }

    private void business() {

        MyTextWatcher myTextWatcher = new MyTextWatcher();
        MyTextWatcher1 myTextWatcher1 = new MyTextWatcher1();
        MyTextWatcher2 myTextWatcher2 = new MyTextWatcher2();
        et_pw.addTextChangedListener(myTextWatcher);
        et_pwagain.addTextChangedListener(myTextWatcher1);
        et_email.addTextChangedListener(myTextWatcher2);

        btn_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                clientSocket = SocketFactory.getSockt();
                TaskBusiness taskBusiness = TaskBusiness.getInstance();
                taskBusiness.doAuth(RegisthreeActivity.this, "01", "2010", phone, pw, new MinaResultLIstener() {
                    @Override
                    public void messageReceived(IoSession session, Object msg) {
                        MianManager.getManager().shutDown();
                        LogUtils.v(TAG + "resultConten:" + msg);
//                      完成注册跳转到注册页面
//                        String totallength = resultContent.substring(0, 8);
//                        String version = resultContent.substring(8, 12);
//                        String commandid = resultContent.substring(12, 16);
//                        String orderid = resultContent.substring(16, 20);
//                        String sendercode = resultContent.substring(20, 40);
//                        String timestamp = resultContent.substring(40, 54);
//                        String clientid = resultContent.substring(54, 74);
                        Message message = Message.obtain();
                        String msg1 = msg.toString();
                        if (null != msg) {
                            String result = msg1.substring(74, 76);
                            LogUtils.v(TAG + "result：" + result);
                            if (null != result && result.equals("00")) {

                                message.obj = result;
                                message.what = REGIST_SUCCESS;
                            } else {

                                message.what = REGIST_FAIL;

                            }

                        } else {

                            message.what = REGIST_FAIL;
                        }
                        mHandler.sendMessage(message);
                    }

                    @Override
                    public void exceptionCaught(IoSession session, Throwable cause) {
                        MianManager.getManager().shutDown();

                        LogUtils.v(TAG+"failmsg:"+cause.getMessage());

                    }

                });
            }
        });
    }

    private void finishRegist() {

        Toast.makeText(mContext, "注册成功", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(RegisthreeActivity.this, LoginActivity.class);
        startActivity(intent);
        finish();

    }


    private void initToolbar() {

        tl_regist = findViewById(R.id.tl_regist);
        tl_regist.setTitle("");
        setSupportActionBar(tl_regist);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        tl_regist.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                finish();
            }
        });

    }

    private void initView() {

        img_registone = findViewById(R.id.img_registone);
        img_registwo = findViewById(R.id.img_registwo);
        img_registhree = findViewById(R.id.img_registhree);
        tv_registone = findViewById(R.id.tv_registone);
        tv_registwo = findViewById(R.id.tv_registwo);
        tv_registhree = findViewById(R.id.tv_registhree);
        et_pw = findViewById(R.id.et_pw);
        et_pwagain = findViewById(R.id.et_pwagain);
        et_email = findViewById(R.id.et_email);
        btn_finish = findViewById(R.id.btn_finish);
        btn_finish.setClickable(false);

    }

    private void initRegistNumview() {

        int size = (int) getResources().getDimension(R.dimen.img_regist_num_size) * 2;
        LogUtils.v(TAG + "size:" + size);
        int backgroudselect = getResources().getColor(R.color.deepblue);
        int backgroudnoselect = getResources().getColor(R.color.gray);
        Bitmap bitmap1 = BitmapUtils.getNumberBitmap(size, "1", backgroudselect, Color.WHITE);
        img_registone.setImageBitmap(bitmap1);
        Bitmap bitmap2 = BitmapUtils.getNumberBitmap(size, "2", backgroudselect, Color.WHITE);
        img_registwo.setImageBitmap(bitmap2);
        Bitmap bitmap3 = BitmapUtils.getNumberBitmap(size, "3", backgroudselect, Color.WHITE);
        img_registhree.setImageBitmap(bitmap3);
        tv_registone.setTextColor(backgroudselect);
        tv_registwo.setTextColor(backgroudselect);
        tv_registhree.setTextColor(backgroudselect);

    }

    public class MyTextWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            LogUtils.v(TAG + "s:" + s);
            pw = s.toString().trim();
            pwagain = et_pwagain.getText().toString().trim();
            email = et_email.getText().toString().trim();
            canClick(pw, pwagain, email);
        }

        @Override
        public void afterTextChanged(Editable s) {


        }
    }

    public class MyTextWatcher1 implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            LogUtils.v(TAG + "s:" + s);
            pw = et_pw.getText().toString().trim();
            pwagain = s.toString().trim();
            email = et_email.getText().toString().trim();
            LogUtils.v(TAG + "email:" + email);
            canClick(pw, pwagain, email);
        }

        @Override
        public void afterTextChanged(Editable s) {


        }
    }


    public class MyTextWatcher2 implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            LogUtils.v(TAG + "s:" + s);
            pw = et_pw.getText().toString().trim();
            pwagain = et_pwagain.getText().toString().trim();
            email = s.toString().trim();
            canClick(pw, pwagain, email);
        }

        @Override
        public void afterTextChanged(Editable s) {


        }
    }

    private void canClick(String pw, String pwagain, String email) {


        if (null == pw || "".equals(pw) || null == pwagain || "".equals(pwagain) || null == email || "".equals(email)) {

            btn_finish.setBackground(getDrawable(R.drawable.btn_no_click_auth));
            btn_finish.setEnabled(false);

        } else {

            btn_finish.setBackground(getDrawable(R.drawable.btn_click_auth));
            btn_finish.setEnabled(true);
        }


    }
}
